package com.woc.cloud.admin.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import com.woc.cloud.admin.constant.ServiceUrl;
import com.woc.cloud.admin.entity.Response;

/**
 * 功能管理控制器
 * 
 * @author     woc
 * @date       2024-01-26 
 * @version    0.0.1
 */
@RestController
@RequestMapping(value = "/function")
public class FunctionController {
	
	@Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = {"", "/index"}, method = RequestMethod.GET)
    public Object index() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("function/index");
        return mav;
    }

    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public Object list(@RequestParam(required = false, defaultValue = "0") Integer offset,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
    	String url = ServiceUrl.FUNC_LIST + "?offset=" + offset;
    	url += "&limit=" + limit;
        return restTemplate.getForObject(url, Map.class);
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public Object info(Integer id) {
    	String url = ServiceUrl.FUNC_INFO + "?id=" + id;
        return restTemplate.getForObject(url, Response.class);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Object save(@RequestBody Object func) {
        return restTemplate.postForObject(ServiceUrl.FUNC_SAVE, func, Response.class);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(Integer id) {
    	String url = ServiceUrl.FUNC_DELETE + "?id=" + id;
        return restTemplate.postForObject(url, null, Response.class);
    }
}
